package com.gewu.pm.ui.adapter.order;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.LabelBean;

/**
 * 标签适配器
 */
public final class LabelAdapter extends AppAdapter<LabelBean> {

    private OnLabelClickListener mListener;

    public LabelAdapter(Context context) {
        super(context);
    }

    public void setOnLabelClickListener(OnLabelClickListener listener) {
        mListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new LabelViewHolder();
    }

    private final class LabelViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView mLabelView;

        private LabelViewHolder() {
            super(R.layout.label_item);
            mLabelView = findViewById(R.id.tv_label);
            mLabelView.setOnClickListener(v -> {
                if (mListener != null) {
                    mListener.onLabelClick(getViewHolderPosition());
                }
            });
        }

        @Override
        public void onBindView(int position) {
            LabelBean label = getItem(position);
            mLabelView.setText(label.getName());
            mLabelView.setSelected(label.isSelected());
        }
    }

    /**
     * 标签点击回调接口
     */
    public interface OnLabelClickListener {
        void onLabelClick(int position);
    }
}

